package com.engine4cloud.tdf.supervision.service.impl;

import com.engine4cloud.tdf.common.core.constant.enums.AllAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.constant.enums.AuditBusinessTypeEnum;
import com.engine4cloud.tdf.common.core.constant.enums.SimpleAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.supervision.dto.SpAuditDTO;
import com.engine4cloud.tdf.supervision.dto.create.SpAuditResultCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpEmergencyReportResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpSimplePreActionUpdateDTO;
import com.engine4cloud.tdf.supervision.service.SpAuthorizeService;
import com.engine4cloud.tdf.supervision.service.SpEmergencyReportAuditService;
import com.engine4cloud.tdf.supervision.service.SpEmergencyReportService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class SpEmergencyReportAuditServiceImpl implements SpEmergencyReportAuditService {
    @Resource
    private SpAuthorizeService authorizeService;
    @Resource
    private SpEmergencyReportService emergencyReportService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean authorize(SpAuditDTO dto) {
        SpEmergencyReportResultDTO old = Optional.ofNullable(emergencyReportService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        emergencyReportService.changeAction(new SpSimplePreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), SimpleAuditActionStatusEnum.AUTHORIZE));
        return authorizeService.authorize(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.EMERGENCY_REPORT, AllAuditActionStatusEnum.AUTHORIZE));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean reject(SpAuditDTO dto) {
        SpEmergencyReportResultDTO old = Optional.ofNullable(emergencyReportService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        emergencyReportService.changeAction(new SpSimplePreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), SimpleAuditActionStatusEnum.REJECT));
        return authorizeService.reject(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.EMERGENCY_REPORT, AllAuditActionStatusEnum.REJECT));
    }
}
